require 'rubygems'
require 'fileutils'
require 'json'

$curdir = pwd
TARGETDIR = ENV['TARGET_TEMP_DIR']
raise "TARGET_TEMP_DIR is not set" if TARGETDIR.nil?
OBJDIR = ENV['TEMP_FILES_DIR']
raise "TEMP_FILES_DIR is not set" if OBJDIR.nil?
$rootdir = ENV['RHO_ROOT']
raise "RHO_ROOT is not set" if $rootdir.nil?
$appincdir = ENV["RHO_INC"]
raise "RHO_INC is not set" if $appincdir.nil?
$androidndkpath = ENV['ANDROID_NDK']
raise 'ANDROID_NDK is not set' if $androidndkpath.nil?
$androidsdk = ENV['ANDROID_SDK']
raise 'ANDROID_SDK is not set' unless $androidsdk    
TARGETLIB = ENV['TARGETLIB']
raise 'TARGETLIB is not set' if ENV['TARGETLIB'].nil?
$app_dir = ENV["RHO_APP_DIR"]
raise "RHO_APP_DIR is not set" if $app_dir.nil?

require File.join($rootdir, 'platform/android/build/androidcommon.rb')
ARGS = [
  "#{$curdir}/ext/android/jni/src",
  "#{$rootdir}/platform/android/Rhodes/jni/include",
  "#{$rootdir}/platform/android/Rhodes/jni/include/rhodes/details",
  "#{$rootdir}/platform/shared/ruby/include",
  "#{$rootdir}/platform/shared",
  "#{$rootdir}/platform/shared/common",
  "#{$rootdir}/platform/shared/api_generator",
  "#{$appincdir}",
  "#{$rootdir}/platform/shared/ruby",
  "#{$rootdir}/platform/shared/ruby/android",
  "#{$rootdir}/platform/shared/ruby/generated",
  "#{$rootdir}/lib/commonAPI/coreapi/ext/shared"
].map{|i| "-I\"#{i}\""}

OBJDIR_ARM = File.join(OBJDIR,'arm')
OBJDIR_x86 = File.join(OBJDIR,'x86')
OBJDIR_MIPS = File.join(OBJDIR,'mips')
OBJDIR_AARCH64 = File.join(OBJDIR,'aarch64')

SRC = get_sources File.join($curdir, 'ext_native.files')

OBJ_ARM = FileList.new(get_objects(SRC, OBJDIR_ARM))
OBJ_x86 = FileList.new(get_objects(SRC, OBJDIR_x86))
OBJ_MIPS = FileList.new(get_objects(SRC, OBJDIR_MIPS))
OBJ_AARCH64 = FileList.new(get_objects(SRC, OBJDIR_AARCH64))

TARGETDIR_ARM = File.join(TARGETDIR,'armeabi')
TARGETDIR_x86 = File.join(TARGETDIR,'x86')
TARGETDIR_MIPS = File.join(TARGETDIR,'mips')
TARGETDIR_AARCH64 = File.join(TARGETDIR,'aarch64')

TARGETLIB_ARM = File.join(TARGETDIR_ARM,TARGETLIB)
TARGETLIB_x86 = File.join(TARGETDIR_x86,TARGETLIB)
TARGETLIB_MIPS = File.join(TARGETDIR_MIPS,TARGETLIB)
TARGETLIB_AARCH64 = File.join(TARGETDIR_AARCH64,TARGETLIB)
#FCMJAR = File.join(TARGETDIR,'fcm.jar')

cd '../..'

namespace 'config' do
  task :arm do
      setup_ndk(ENV['ANDROID_NDK'],ENV['ANDROID_API_LEVEL'],'arm')
  end
  
  task :x86 do
      setup_ndk(ENV['ANDROID_NDK'],ENV['ANDROID_API_LEVEL'],'x86')
  end
  
  task :mips do
      setup_ndk(ENV['ANDROID_NDK'],ENV['ANDROID_API_LEVEL'],'mips')
  end

  task :aarch64 do
    setup_ndk(ENV['ANDROID_NDK'],ENV['ANDROID_API_LEVEL'],'aarch64')
  end
end

directory TARGETDIR
directory TARGETDIR_ARM => TARGETDIR
directory TARGETDIR_x86 => TARGETDIR
directory TARGETDIR_MIPS => TARGETDIR
directory TARGETDIR_AARCH64 => TARGETDIR
directory OBJDIR_ARM
directory OBJDIR_x86
directory OBJDIR_MIPS
directory OBJDIR_AARCH64

rule '.o' => lambda {|obj| find_source(obj) } do |t|
  abi = File.basename(File.dirname(t.name))
  Rake::Task["config:#{abi}"].invoke
  cc_compile find_source(t.name), File.dirname(t.name), ARGS or raise "\e[31mCompilation failed: #{find_source(t.name)}\e[0m"
end

task OBJ_ARM => OBJDIR_ARM
task OBJ_x86 => OBJDIR_x86
task OBJ_MIPS => OBJDIR_MIPS
task OBJ_AARCH64 => OBJDIR_AARCH64

def find_source(objfile)
  base = File.basename(objfile, '.o')
  src = SRC.find { |s| File.basename(s) == base }
  raise "\e[31mSource does not exist: #{src}\e[0m" unless File.exist?(src)
  src
end

file TARGETLIB_ARM => TARGETDIR_ARM do |t|
    Rake::Task["config:arm"].invoke
    cc_ar t.name, OBJ_ARM or raise "\e[31mCreating library failed: #{t.name}\e[0m"
end
multitask TARGETLIB_ARM => OBJ_ARM

file TARGETLIB_x86 => TARGETDIR_x86 do |t|
    Rake::Task["config:x86"].invoke
    cc_ar t.name, OBJ_x86 or raise "\e[31mCreating library failed: #{t.name}\e[0m"
end
multitask TARGETLIB_x86 => OBJ_x86

file TARGETLIB_MIPS => TARGETDIR_MIPS do |t|
    Rake::Task["config:mips"].invoke
    cc_ar t.name, OBJ_MIPS or raise "\e[31mCreating library failed: #{t.name}\e[0m"
end
multitask TARGETLIB_MIPS => OBJ_MIPS

file TARGETLIB_AARCH64 => TARGETDIR_AARCH64 do |t|
  Rake::Task["config:aarch64"].invoke
  cc_ar t.name, OBJ_AARCH64 or raise "\e[31mCreating library failed: #{t.name}\e[0m"
end
multitask TARGETLIB_AARCH64 => OBJ_AARCH64






###############

$xmlFileWithDataFromJson = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>"

def add_data_to_string(name, dataFromJson)
  $xmlFileWithDataFromJson << "\n\t<string name=\"" << name << "\" translatable=\"false\">" << dataFromJson << "</string>"
end

$jsonfile = File.read(File.join($app_dir, 'google-services.json')) if File.exist?(File.join($app_dir, 'google-services.json'))

if $jsonfile.nil? 

  if ENV["FCM_MPBILESDK_APP_ID"].nil?
    puts "Please set mobilesdk_app_id variable in android/fcm section of build.yml"
  end
  if ENV["FCM_PROJECT_NUMBER"].nil?
    puts "Please set project_number variable in android/fcm section of build.yml"
  end
  if ENV["FCM_CURRENT_KEY"].nil?
    puts "Please set current_key variable in android/fcm section of build.yml"
  end
  if ENV["FCM_PROJECT_ID"].nil?
    puts "Please set project_id variable in android/fcm section of build.yml"
  end
  
  if ENV["FCM_MPBILESDK_APP_ID"].nil? or ENV["FCM_PROJECT_NUMBER"].nil? or ENV["FCM_CURRENT_KEY"].nil? or ENV["FCM_PROJECT_ID"].nil?
    exit 1
  end


  add_data_to_string("google_app_id", ENV["FCM_MPBILESDK_APP_ID"])
  add_data_to_string("gcm_defaultSenderId", ENV["FCM_PROJECT_NUMBER"])
  add_data_to_string("google_api_key", ENV["FCM_CURRENT_KEY"])
  add_data_to_string("project_id", ENV["FCM_PROJECT_ID"])

else
  begin 
    jsonHash = JSON.parse($jsonfile)

    puts "Creating values.xml from google-sevices.json" if $jsonfile

    add_data_to_string("google_app_id", jsonHash["client"][0]["client_info"]["mobilesdk_app_id"])
    add_data_to_string("gcm_defaultSenderId", jsonHash["project_info"]["project_number"])
    add_data_to_string("default_web_client_id", jsonHash["client"][0]["oauth_client"][0]["client_id"])
    add_data_to_string("google_api_key", jsonHash["client"][0]["api_key"][0]["current_key"])
    add_data_to_string("google_crash_reporting_api_key", jsonHash["client"][0]["api_key"][0]["current_key"])
    add_data_to_string("project_id", jsonHash["project_info"]["project_id"])

  rescue Exception => e
    puts "\nCan't create id values for firebase. Please check your google-services.json.\n"
    puts e
    exit 1
  end
end

$xmlFileWithDataFromJson << "\n</resources>"
resGoogleDir = File.join($curdir, 'res', 'res', 'values')

FileUtils.mkdir_p(resGoogleDir) unless File.exist?(resGoogleDir)
File.open(File.join(resGoogleDir, 'fireBaseValues.xml'), 'w') {|f| f.write($xmlFileWithDataFromJson) }
puts "fireBaseValues.xml created"

##############


namespace 'arch' do
  task :arm => [TARGETLIB_ARM]
  task :x86 => [TARGETLIB_x86]
  task :mips => [TARGETLIB_MIPS]
  task :aarch64 => [TARGETLIB_AARCH64]
end

task :default => ["arch:arm", "arch:x86"]
